﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace _MetalSlug
{
    public class MySprite : VisibleGameEntity
    {
        public enum FLIPABLE
        {
            FLIP,
            NOT_FLIP,
            UNKNOWN
        }

        protected FLIPABLE _flip = FLIPABLE.UNKNOWN;
        private float _rotation = 0;
        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }
        public MySprite()
        {
          
        }

        protected List<Texture2D> _Textures;
        public List<Texture2D> Textures
        {
            get { return _Textures; }
            set
            {
                _Textures = value;
                ITexture = 0; 
            }
        }

        protected int _iTexture;
        public int ITexture
        {
            get { return _iTexture; }
            set { _iTexture = value; }
        }

        public FLIPABLE Flip
        {
            get { return _flip; }
            set { _flip = value; }
        }

        private Vector2 _size = new Vector2(0, 0);
        public MySprite(List<Texture2D> textures, Vector2 pos)
        {
            Textures = textures;
            TopLeft = pos;
        }

        public Color color = Color.White;
        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Vector2 newPos = TopLeft - new Vector2(GameUtil.GamePivot.PivotPoint, 0);
            Rectangle rec = new Rectangle();
            if (Size.X == 0)
            {
                rec = new Rectangle((int)newPos.X, (int)newPos.Y, Textures[ITexture].Width, Textures[ITexture].Height);
            }
            else
            {
                rec = new Rectangle((int)newPos.X, (int)newPos.Y, (int)Size.X, (int)Size.Y);

            }

            switch(Flip)
            {
                case FLIPABLE.FLIP:
                    spriteBatch.Draw(_Textures[ITexture], newPos, null, Color, Rotation, Vector2.Zero, 1.0f, SpriteEffects.FlipHorizontally, 0f);
                    break;

                case FLIPABLE.NOT_FLIP:
                    spriteBatch.Draw(_Textures[ITexture], newPos, null, Color, Rotation, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
                    break;

                case FLIPABLE.UNKNOWN:
                    spriteBatch.Draw(_Textures[ITexture], rec, Color.Lerp(Color.White, Color.Transparent, mAlphaValue));
                    break;
            }
        }

        float mAlphaValue = 0;
        public void setAlphaValue(float value)
        {
            mAlphaValue = value;
        }


        float mFadeIncrement = .035f;
        public void nextFrame()
        {
            ITexture = (ITexture + 1) % Textures.Count;
        }

        public bool isLastFrame()
        {
            if (ITexture == Textures.Count - 1)
                return true;
            return false; 
        }
        public void setFrame(int value)
        {
            ITexture = value;
        }
        private GameTime gameTime;
        private float RotationAngle;
        
        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            // rotation 
            RotationAngle += elapsed;
            float circle = MathHelper.Pi * 2;
            RotationAngle = RotationAngle % circle;

            this.gameTime = gameTime;
            //Increment/Decrement the fade value for the image
            mAlphaValue += mFadeIncrement;

            //If the AlphaValue is equal or above the max Alpha value or
            //has dropped below or equal to the min Alpha value, then 
            //reverse the fade
            if (mAlphaValue >= 1 || mAlphaValue <= 0)
            {
                mFadeIncrement *= -1;
                mAlphaValue = MathHelper.Clamp(mAlphaValue, 0, 1);
            }
        }

       
    }
}
